package com.joseph.websocket.util;

import com.alibaba.fastjson.JSON;
import com.joseph.websocket.bean.Message;
import com.vdurmont.emoji.EmojiParser;
import io.netty.util.AttributeKey;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.query.QuerySearchRequest;
import org.omg.PortableInterceptor.INACTIVE;

import java.io.IOException;

public class ESUtil {

    static RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
            RestClient.builder(new HttpHost("172.16.0.51", 9200,"http"))
    );


    public static void main(String[] args) throws IOException {
        IndexRequest indexRequest = new IndexRequest("index_joseph", "index_joseph", "1");
//        // 创建员工信息
//        Message message = new Message();
//        message.setContent("hahaha");
//        message.setMsgTime(System.currentTimeMillis());
//        // 将对象转换为 byte 数组
//        byte[] json = JSON.toJSONBytes(message);
//        // 设置文档内容
//        indexRequest.source(json, XContentType.JSON);
//        // 执行增加文档
//        IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
//
//
//        // 获取请求对象
//        GetRequest getRequest = new GetRequest("index_joseph", "doc", "1");
//        // 获取文档信息
//        GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
//
//        System.out.println(getResponse.getSourceAsString());

        DeleteRequest deleteRequest = new DeleteRequest("index_joseph", "doc", "1");
        restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);

    }
}
